package com.myapp.filter;

import java.io.IOException;
import java.util.List;

import javax.ejb.EJB;
import javax.inject.Inject;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.camunda.bpm.engine.IdentityService;
import org.camunda.bpm.engine.cdi.BusinessProcess;
import org.camunda.bpm.engine.impl.identity.Authentication;
import org.camunda.bpm.engine.task.Task;

import com.myapp.business.EmployeeGetter;
import com.myapp.db.ProcessEngineDataGetter;

//@WebFilter("/pages/*")
public class LoginFilter implements Filter {
	@Inject
	private BusinessProcess businessProcess;
	
	@Inject
	private IdentityService identityService;
	
	@EJB
	private ProcessEngineDataGetter processEngineDataGetter;
	
	@EJB
	private EmployeeGetter employeeGetter;

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException,
			ServletException {
		System.out.println("======= LoginFilter ========");
		
		List<Object[]> resultList = processEngineDataGetter.getSomething();
		System.out.println(resultList);
		
		// Test getting assignee.
		Task task = businessProcess.getTask();
		if(task != null) {
			String assignee = task.getAssignee();
			System.out.println(assignee);
		}
		
		// Test getting current user.
		Authentication currentUser = identityService.getCurrentAuthentication();
		if(currentUser != null) {
			String userId = currentUser.getUserId();
			System.out.println(userId);
		}
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
	}
	
	@Override
	public void destroy() {
	}
}
